package com.xbongbong.sso.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.sso.pojo.dto.OauthCustomParamAddDTO;
import com.xbongbong.sso.pojo.dto.OauthCustomParamDeleteDTO;
import com.xbongbong.sso.pojo.dto.OauthCustomParamDetailDTO;
import com.xbongbong.sso.pojo.dto.OauthCustomParamListDTO;
import com.xbongbong.sso.pojo.dto.OauthCustomParamUpdateDTO;
import com.xbongbong.sso.pojo.dto.OauthFieldListDTO;
import com.xbongbong.sso.pojo.dto.OauthSyntaxCheckDTO;
import com.xbongbong.sso.pojo.vo.OauthCustomParamAddVO;
import com.xbongbong.sso.pojo.vo.OauthCustomParamDetailVO;
import com.xbongbong.sso.pojo.vo.OauthCustomParamListVO;
import com.xbongbong.sso.pojo.vo.OauthCustomParamUpdateVO;
import com.xbongbong.sso.pojo.vo.OauthFieldListVO;
import com.xbongbong.sso.pojo.vo.OauthFunctionlistVO;
import com.xbongbong.sso.service.OauthCustomParamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 授权自定义参数
 * 创建时间 2020/12/28 2:51 PM
 * 修改时间 2020/12/28 2:51 PM
 * @author chy
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/oauthCustomParam")
public class OauthCustomParamController {

    private static final Logger LOG = LoggerFactory.getLogger(OauthCustomParamController.class);

    @Resource
    private OauthCustomParamService oauthCustomParamService;

    /**
     * 授权自定义参数列表
     * @param oauthCustomParamListDTO 关联DTO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid OauthCustomParamListDTO oauthCustomParamListDTO, BindingResult br) throws Exception{
        XbbResponse<OauthCustomParamListVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthCustomParamListVO oauthCustomParamListVO = oauthCustomParamService.list(oauthCustomParamListDTO);
                response = new XbbResponse<>(oauthCustomParamListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 字段列表
     * @param oauthFieldListDTO 关联DTO
     */
    @RequestMapping(value = "/fieldlist", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fieldlist(@RequestBody @Valid OauthFieldListDTO oauthFieldListDTO, BindingResult br) throws Exception{
        XbbResponse<OauthFieldListVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthFieldListVO oauthFieldListVO = oauthCustomParamService.fieldlist(oauthFieldListDTO);
                response = new XbbResponse<>(oauthFieldListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 函数列表
     * @param baseDTO 关联DTO
     */
    @RequestMapping(value = "/functionlist", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String functionlist(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) throws Exception{
        XbbResponse<OauthFunctionlistVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthFunctionlistVO oauthFunctionlistVO = oauthCustomParamService.functionlist();
                response = new XbbResponse<>(oauthFunctionlistVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 公式验证
     * @param oauthSyntaxCheckDTO 关联DTO
     */
    @RequestMapping(value = "/syntaxCheck", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String syntaxCheck(@RequestBody @Valid OauthSyntaxCheckDTO oauthSyntaxCheckDTO, BindingResult br) throws Exception{
        XbbResponse<BaseVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = oauthCustomParamService.syntaxCheck(oauthSyntaxCheckDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权自定义参数新增
     * @param oauthCustomParamAddDTO 关联DTO
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid OauthCustomParamAddDTO oauthCustomParamAddDTO, BindingResult br) throws Exception{
        XbbResponse<OauthCustomParamAddVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthCustomParamAddVO oauthCustomParamAddVO = oauthCustomParamService.add(oauthCustomParamAddDTO);
                response = new XbbResponse<>(oauthCustomParamAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权自定义参数详情
     * @param oauthCustomParamDetailDTO 关联DTO
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detail(@RequestBody @Valid OauthCustomParamDetailDTO oauthCustomParamDetailDTO, BindingResult br) throws Exception{
        XbbResponse<OauthCustomParamDetailVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthCustomParamDetailVO oauthCustomParamDetailVO = oauthCustomParamService.detail(oauthCustomParamDetailDTO);
                response = new XbbResponse<>(oauthCustomParamDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权自定义参数更新
     * @param oauthCustomParamUpdateDTO 关联DTO
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid OauthCustomParamUpdateDTO oauthCustomParamUpdateDTO, BindingResult br) throws Exception{
        XbbResponse<OauthCustomParamUpdateVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthCustomParamUpdateVO oauthCustomParamUpdateVO = oauthCustomParamService.update(oauthCustomParamUpdateDTO);
                response = new XbbResponse<>(oauthCustomParamUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权自定义参数删除
     * @param oauthCustomParamDeleteDTO 关联DTO
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid OauthCustomParamDeleteDTO oauthCustomParamDeleteDTO, BindingResult br) throws Exception{
        XbbResponse<BaseVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = oauthCustomParamService.delete(oauthCustomParamDeleteDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
